package edu.suda.xsap.controller;

import edu.suda.xsap.common.Result;
import edu.suda.xsap.dto.EmployeeDto;
import edu.suda.xsap.entity.Employee;
import edu.suda.xsap.entity.vo.IdVo;
import edu.suda.xsap.entity.vo.Page;
import edu.suda.xsap.entity.vo.TeacherVo;
import edu.suda.xsap.service.EmployeeService;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/system-settings")
@Slf4j

public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    @Operation(summary = "修改老师信息")
    /**
     axios.post("/api/system-settings/Teacher-management",form)  对老师信息进行修改，form为老师信息的json
     */
    @PostMapping("/Teacher-management/update")
    //从前端接受json
    public Result update(@RequestBody Employee employee) {
        try {

            employee.setLastModifyTime(LocalDateTime.now());
            employeeService.update(employee);
            return Result.success();
        } catch (Exception e) {
            log.error("添加失败", e);
            return Result.failure(e.getMessage(), e);
        }
    }

/**axios.post("/api/system-settings/Teacher-management/getOnePageDate",currentPage.value)
 发送页码，（默认为每页10条，不足10条则全部返回）*/
    @PostMapping("/Teacher-management/getOnePageDate")
    public Result getOnePageDate(@RequestBody Page pages) {
        try {
            System.out.println(pages);
            Integer page=pages.getPage();
            System.out.println(page);
            List<EmployeeDto> lst = employeeService.findByLimit(10, (page - 1) * 10);
            System.out.println(lst);
            return Result.success(lst);
        } catch (Exception e) {
            log.error("查询失败", e);
            return Result.failure(e.getMessage(), e);
        }
    }

    /*axios.get("/api/system-settings/Teacher-management/get10")**/
    @GetMapping("/Teacher-management/get10")
    public Result get10() {
        try {
            List<EmployeeDto> lst = employeeService.findByLimit(10, 0);
            return Result.success(lst);
        } catch (Exception e) {
            log.error("查询失败", e);
            return Result.failure(e.getMessage(), e);
        }
    }

    /**axios.get("/api/system-settings/Teacher-management/getlength")
     获取老师数据的条数，用于生成页数**/
    @GetMapping("/Teacher-management/getlength")
    public Result getlength() {
        try {
            Integer length = employeeService.findCount();
            return Result.success(length);
        } catch (Exception e) {
            log.error("查询失败", e);
            return Result.failure(e.getMessage(), e);
        }
    }

    /**axios.post("/api/system-settings/Teacher-management/check",id)返回该id的老师的基本信息，包括以下：*/
    @PostMapping("/Teacher-management/check")
    public Result check(@RequestBody IdVo idvo) {
        try {
            Integer id = idvo.getId();
            EmployeeDto employeeDto = employeeService.findById(id);
            List<TeacherVo> teacherVo = employeeService.record(id);
            employeeDto.setTeacherVoList(teacherVo);
            System.out.println(employeeDto);
            return Result.success(employeeDto);
        } catch (Exception e) {
            log.error("查询失败", e);
            return Result.failure(e.getMessage(), e);
        }
    }

    /**axios.post("/api/system-settings/Teacher-management/add",form)*/
    @PostMapping("/Teacher-management/add")
    public Result add(@RequestBody Employee employee) {
        try {
            System.out.println(employee);
            employee.setCreateTime(LocalDateTime.now());
            employee.setLastModifyTime(LocalDateTime.now());
            employeeService.save(employee);
            return Result.success();
        } catch (Exception e) {
            log.error("添加失败", e);
            return Result.failure(e.getMessage(), e);
        }
    }
    /**axios.post("/api/system-settings/Teacher-management/delete"*/
    @PostMapping("/Teacher-management/delete")
    public Result delete(@RequestBody IdVo idvo) {
        try {
            Integer id = idvo.getId();
            System.out.println(id);
            employeeService.delete(id);
            return Result.success();
        } catch (Exception e) {
            log.error("删除失败", e);
            return Result.failure(e.getMessage(), e);
        }
    }
}
